package ctf.evaluation.SimulatorTests;

import java.io.IOException;

import ctf.evaluation.Point;
import ctf.evaluation.TestCase;
import ctf.evaluation.simulator.ConnectionManager;
import ctf.evaluation.simulator.ConnectionManager.STEALState;
import ctf.evaluation.simulator.data.Player;
import ctf.evaluation.simulator.data.State;
import ctf.evaluation.simulator.responses.CommandResponse;
import ctf.evaluation.simulator.responses.GameOverException;
import ctf.evaluation.simulator.responses.RoundOverException;
import ctf.evaluation.simulator.responses.StateResponse;
import ctf.model.Side;
import ctf.network.NetworkException;
import ctf.network.ProtocolError;
import ctf.network.SocketClosedException;
import ctf.network.UninitializedSocketException;
import ctf.network.tcp.Connection;


public class Scoring extends TestCase {

	private ConnectionManager connections = ConnectionManager.instance();
	private Connection red, blue;
	
	@Override
	public void setUp() throws Exception {
		connections.enterState(STEALState.PLAY);
		red = connections.red();
		blue = connections.blue();
	}
	
//	public void testInitialScore()throws GameOverException,RoundOverException, ProtocolError, NetworkException, SocketClosedException, UninitializedSocketException, IOException{
//		ctf.evaluation.simulator.data.State state;		
//		
//		//Check score starts as 0-0
//		red.sendLine("getState");
//		state = (new StateResponse(red)).state();
//		assertTrue(state.team(Side.LEFT).score() == 0 && state.team(Side.RIGHT).score() == 0);
//	
//	}
//	
//	public void testPointsForCapturingPlayer() throws GameOverException,RoundOverException, ProtocolError, NetworkException, SocketClosedException, UninitializedSocketException, IOException{		
//		ctf.evaluation.simulator.data.State state;
//				
//		//Capture a player
//		red.sendLine("accelerate 7 0 2.0");
//		sleep(50000);		
//		
//		//Check if a point was awarded for capturing a player
//		red.sendLine("getState");
//		state = (new StateResponse(red)).state();
//		assertTrue(state.team(Side.LEFT).score() == 1 || state.team(Side.RIGHT).score() == 1);		
//	}
//	
//	public void testPointsForCapturingEntireTeam()throws GameOverException,RoundOverException, ProtocolError, NetworkException, SocketClosedException, UninitializedSocketException, IOException{
//		ctf.evaluation.simulator.data.State state;
//		
//		//Capture all player
//		red.sendLine("accelerate 0 0 2.0");
//		red.sendLine("accelerate 1 0 2.0");
//		red.sendLine("accelerate 2 0 2.0");
//		red.sendLine("accelerate 3 0 2.0");
//		red.sendLine("accelerate 4 0 2.0");
//		red.sendLine("accelerate 5 0 2.0");
//		red.sendLine("accelerate 6 0 2.0");
//		red.sendLine("accelerate 7 0 2.0");		
//		sleep(50000);		
//		
//		//Check if a point was awarded for capturing each player and an additional 2
//		red.sendLine("getState");
//		state = (new StateResponse(red)).state();
//		assertTrue(state.team(Side.LEFT).score() == 10 || state.team(Side.RIGHT).score() == 10);		
//		}
	
	public void testCaptureFlagPoints() throws IOException, NetworkException, ProtocolError, RoundOverException, GameOverException {
		Point playerPosition = new Point(401, 125);
		red.sendLine("placePlayer 0 " + playerPosition .toSTEAL());
		CommandResponse response = new CommandResponse(red);
		red.sendLine("spin 0 0 90");
		response = new CommandResponse(red);
		while(true){
			sleep(100);
			red.sendLine("getState");
			State state = (new StateResponse(red)).state();
			Player player = state.team(Side.LEFT).player(0);
			Double orientation = player.heading();
			if(orientation > 179)
				break;
		}
		red.sendLine("spin 0 0 0");
		response = new CommandResponse(red);
		red.sendLine("accelerate 0 0 2.0");
		response = new CommandResponse(red);
		sleep(60000);
		
	}
}
